<template>
  <div>
    <Header :contentTitle="headerTitle"></Header>
    <div class="home-wrapper">
      <scroller :on-infinite="handleReachButton">
        <div class="homestr">
          <div class="activity-info-box">
            <div class="study-info" v-for="item in getActivityData" :key="item.id" @click="godetail(item.id)">
              <span class="title">{{ item.title }}</span>
              <div class="desc flex-h">
                <span class="left">
                  <!-- <img src="~@/static/starts.png" alt />
                  <i>{{item.likeCount}}</i> -->
                </span>
                <span class="flex1 right">
                  <b class="flex1">{{ headerTitle }}</b>
                  <em>{{ new Date(item.createTime).format('MM-dd') }}</em>
                </span>
              </div>
            </div>
          </div>
        </div>
      </scroller>
    </div>
  </div>
</template>

<script>
import Header from '~/components/topHeader'
import { getResearchInfoByPage, getResearchPolicyByPage } from '~/api/identity'
import ls from 'store2'
import { format } from '~/plugins/filter'
export default {
  components: {
    Header,
  },
  data() {
    return {
      headerTitle: '研学资讯',
      productList: [],
      currentPage: 1,
      getActivityData: [],
    }
  },
  mounted() {
    this.$visit(this.$route.path)
    if (this.$route.query.type === 'policy') {
      this.headerTitle = '政策资讯'
      this.getResearchPolicyByPage()
    } else {
      this.getALlActivityList()
    }
  },
  methods: {
    godetail(id) {
      this.$router.push({ path: 'infoDetail?id=' + id + '&type=' + this.$route.query.type })
    },
    getResearchPolicyByPage() {
      this.$loading('open')
      getResearchPolicyByPage({
        pageNo: 1,
        pageSize: 10,
      })
        .then((res) => {
          this.getActivityData = res.data.list
          this.totalPages = res.data.pages
          this.$hideLoading()
        })
        .catch((error) => {
          this.$hideLoading()
        })
    },
    getALlActivityList() {
      this.$loading('open')
      getResearchInfoByPage({
        pageNo: 1,
        pageSize: 10,
      })
        .then((res) => {
          this.getActivityData = res.data.list
          this.totalPages = res.data.pages
          this.$hideLoading()
        })
        .catch((error) => {
          this.$hideLoading()
        })
    },
    handleReachButton(done) {
      if (this.currentPage < this.totalPages) {
        this.currentPage = this.currentPage + 1
        if (this.$route.query.type === 'policy') {
          getResearchPolicyByPage({
            pageNo: this.currentPage,
            pageSize: 10,
          }).then((res) => {
            let newArr = res.data.list
            this.getActivityData = this.getActivityData.concat(newArr)
            this.totalPages = res.data.pages
            done()
          })
        } else {
          getResearchInfoByPage({
            pageNo: this.currentPage,
            pageSize: 10,
          }).then((res) => {
            let newArr = res.data.list
            this.getActivityData = this.getActivityData.concat(newArr)
            this.totalPages = res.data.pages
            done()
          })
        }
      } else {
        done(true)
        return
      }
    },
  },
}
</script>

<style scoped lang="less">
.activity-info-box {
  width: 100%;
  height: auto;

  padding: 0 0.3rem;
  box-sizing: border-box;
  .study-info {
    width: 100%;
    height: auto;
    padding: 0.2rem 0;
    border-bottom: 1px solid rgba(235, 235, 235, 1);
    // starts
    .title {
      display: block;
      font-size: 0.3rem;
      font-weight: 500;
      color: rgba(3, 3, 3, 1);
      line-height: 0.4rem;
    }
    .desc {
      margin-top: 0.2rem;
      .left {
        img {
          width: 0.2rem;
          height: 0.2rem;
        }
        i {
          font-size: 0.2rem;
          color: rgba(235, 78, 62, 1);
        }
      }

      .right {
        text-align: right;
        b {
          font-size: 0.24rem;
          color: rgba(28, 28, 29, 1);
        }
        em {
          font-size: 0.24rem;
          color: rgba(187, 187, 187, 1);
          margin-left: 0.1rem;
        }
      }
    }
  }
}
.home-wrapper {
  width: 100%;
  height: 100vh;
  margin-top: -0.88rem;
  background: #fff;
}
.homestr {
  width: 100%;
  height: auto;
  background: #fff;
  padding-top: 0.88rem;
  box-sizing: border-box;
}
.list-wrapper {
  padding: 0.3rem;
  box-sizing: border-box;
  .act-box {
    width: 100%;
    height: 3.86rem;
    border-radius: 0.1rem;

    position: relative;
    margin-bottom: 0.3rem;

    span {
      position: absolute;
      width: 100%;
      height: 1rem;
      box-sizing: border-box;
      left: 0;
      bottom: 0rem;
      padding: 0.1rem 0.2rem;
      border-bottom-left-radius: 0.1rem;
      border-bottom-right-radius: 0.1rem;
    }
    .bgsp {
      background: rgba(0, 0, 0, 0.3);
    }
    .titlesp {
      font-size: 0.3rem;
      font-family: PingFangSC;
      font-weight: 600;
      //   height: 0.8rem;

      color: rgba(255, 255, 255, 1);
      //   line-height: 0.4rem;

      display: -webkit-box;
      -webkit-box-orient: vertical;
      -webkit-line-clamp: 2;
      display: flex;
      display: -webkit-flex;
      justify-content: left; /*水平居中*/
      align-items: center; /*垂直居中*/
      -webkit-align-items: center;
      overflow: hidden;
      text-overflow: ellipsis;
    }
  }
}
</style>
